﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditionPlace.xaml
    /// </summary>
    public partial class ucAirConditionPlace : UserControl
    {
        bool display = true;
        public ucAirConditionPlace()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            cbxDescription.ItemsSource = LibraryManagerInCom.AirConditionPlaceList;
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }

        public void reloadData()
        {
            cbxDescription.IsEditable = false;
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.AirConditionPlaceList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedIndex >= 0 && display)
            {
                tb_air_conditioning_place ap = (tb_air_conditioning_place)cbxDescription.SelectedItem;
                txtPeopleDensity.Text = ap.people_density.ToString();
                txtAirAmount.Text = ap.air_amount_per_perople.ToString();
                txtLightCoefficient.Text = ap.impact_lighting.ToString();
                txtPeopleCoefficient.Text = ap.impact_people.ToString();

            }

        }
        public void addNew()
        {
            cbxDescription.IsEditable = true;
            cbxDescription.Text = "";
            txtAirAmount.Text = "";
            txtLightCoefficient.Text = "";
            txtPeopleCoefficient.Text = "";
            txtPeopleDensity.Text = "";
            display = false;

        }
        public void saveData()
        {
            tb_air_conditioning_place ap = new tb_air_conditioning_place();
            ap.description = cbxDescription.Text;
            ap.people_density = Convert.ToInt16(txtPeopleDensity.Text);
            ap.air_amount_per_perople = Convert.ToDouble(txtAirAmount.Text);
            ap.impact_people = Convert.ToDouble(txtPeopleCoefficient.Text);
            ap.impact_lighting = Convert.ToDouble(txtLightCoefficient.Text);
            LibraryManagerInCom.addAirConditionSpace(ap);
            cbxDescription.IsEditable = false;
            display = true;
            reloadData();
        }
        public void delData()
        {
            tb_air_conditioning_place ap = (tb_air_conditioning_place)cbxDescription.SelectedItem;
            MessageBoxResult result = MessageBox.Show("Do you want to delte this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if (result == MessageBoxResult.OK)
            {
                LibraryManagerInCom.delAirConditionPlace(ap);
                reloadData();
            }
            
        }

        public void copyData()
        {
            tb_air_conditioning_place ap = (tb_air_conditioning_place)cbxDescription.SelectedItem;
            tb_air_conditioning_place ap2 = LibraryManagerInCom.copyAirCondionPlace(ap);
            cbxDescription.IsEditable = true;
            cbxDescription.Text = ap2.description;
            txtAirAmount.Text = ap2.air_amount_per_perople.ToString();
            txtLightCoefficient.Text = ap2.impact_lighting.ToString() ;
            txtPeopleCoefficient.Text = ap2.impact_people.ToString() ;
            txtPeopleDensity.Text = ap2.people_density.ToString() ;
            

        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}
